package edu.nf.llmtest.web;

import edu.nf.llmtest.common.VO.ResultVO;
import edu.nf.llmtest.common.base.BaseController;
import edu.nf.llmtest.model.dto.EssayDTO;
import edu.nf.llmtest.model.entity.Essay;
import edu.nf.llmtest.service.EssayTypeService;
import edu.nf.llmtest.service.impl.EssayServiceImpl;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @ Author bin
 * @ Create  2025/11/13 9:44
 **/
@RequiredArgsConstructor
@RestController
@RequestMapping("/essay")
public class EssayController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(EssayController.class);
    private final EssayServiceImpl essayService;
    private final EssayTypeService essayTypeService;

    /**
     * 从es获取文章列表 默认每次获取12条
     * @param eIds 已获取浏览过的的文章id
     * @param size 获取文章数量
     * @param type 文章类型
     * @param search 搜索内容
     * */
    @GetMapping("/listEssay")
    public ResultVO<List<EssayDTO>> listEssay(
            @Param("eIds") String eIds,
            @Param("size") Integer size,
            @Param("type") String type,
            @Param("search") String search){
        List<String> split = null;
        if (eIds != null){
           //通过, 将字符串转换成List
           split = List.of(eIds.split(","));
        }
        return success(essayService.listEssay(split, size, type, search));
    }

    @GetMapping("/getEssayById")
    public ResultVO<EssayDTO> getEssayById(@Param("eId") Integer eId){
        return success(essayService.getEssayById(eId));
    }

    /**
     * @param essay 文章信息
     * @param uId 用户id
     * @param tIds 文章类型id集合
     * */

    @GetMapping("/addEssay")
    public ResultVO<Void> addEssay(
            @ModelAttribute("essay") Essay essay,      // 或者直接 Essay essay
            @RequestParam("uId") Integer uId,
            @RequestParam("tIds") List<Integer> tIds   // 或 Integer[] tIds
    ) {
        log.info("添加文章信息: {}", essay);
        log.info("用户id: {}", uId);
        log.info("文章类型id: {}", tIds);

        essayService.addEssay(essay);

        for (Integer tId : tIds) {
            essayTypeService.insertEssayType(uId, tId);
        }
        return success();
    }

}
